﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;

public partial class Manage_currexam : ManagePageBase
{
    int paperid = 0;
    List<Exam.Core.Model.Department> Departlist = new List<Exam.Core.Model.Department>();
    Exam.Core.DAL.StudentExam examDal = new Exam.Core.DAL.StudentExam();
    Exam.Core.DAL.Student stuDal = new Exam.Core.DAL.Student();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            string currpaperid = "0";
            if (!string.IsNullOrEmpty(Request.QueryString["id"])) //得到试卷
            {
                currpaperid = Request.QueryString["id"].Trim();
            }

            if (int.TryParse(currpaperid, out paperid))
            {
                if (paperid > 0)
                {
                    Exam.Core.Model.Paper paper = new Exam.Core.DAL.Paper().getPaperById(paperid);
                    Label1.Text = "<a href=\"paper.aspx?id=" + paper.PaperID + "\" target=_blank title=\"点击预览该试卷\">" + paper.PaperName + "</a>";
                    List<Exam.Core.Model.PaperQuestionType> model = paper.PaperQuestionTypes;
                    if (model == null)
                        return;
                    StringBuilder sb = new StringBuilder();
                    float total = 0;
                    int totalCount = 0;
                    foreach (Exam.Core.Model.PaperQuestionType o in model)
                    {
                        sb.Append(o.QuestionTypeTitle.Trim() + o.QuestionCount.Trim() + "，");
                        total = total + float.Parse(o.Score.Trim()) * int.Parse(o.QuestionCount.Trim());
                        totalCount = totalCount + int.Parse(o.QuestionCount.Trim());
                    }
                    string tmp = Resources.labels.paper_qtotal_score;
                    tmp = string.Format(tmp, totalCount, total);
                    //sb.Append("共计" + totalCount + "道试题，");
                    //sb.Append("总分" + total + "分");
                    sb.Append(tmp);

                    litTj.Text = sb.ToString();

                    BindData();
                }
                else
                {
                    Label1.Text = "还没有设定当前试卷";
                    litTj.Text = "当前试卷是指：考生登录系统后参加考试试卷";
                }

            }

            //部门
            Departlist = new Exam.Core.DAL.Department().getDepartmentAllList(isTheMe.DepartmentID.Trim());


        }
    }

    /// <summary>
    /// 绑定考生
    /// </summary>
    private void BindData()
    {
        //StudentExamPaper

        DataTable dt = new Exam.Core.DAL.StudentExamPaper().getStudentExamPaperByPaperId(paperid);
        rp.DataSource = dt;
        rp.DataBind();

        LibStuCount.Text = dt.Rows.Count.ToString();
        LibStuCount.ForeColor = System.Drawing.Color.Blue;
    }

    protected void rp_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataRowView rw = (DataRowView)e.Item.DataItem;
            string _id = rw["StudentID"].ToString();

            if (_id == "")
                return;

            //Label6
            //Label6
            Exam.Core.Model.Student stu = stuDal.StudentById(int.Parse(_id));
            Label Label6 = (Label)e.Item.FindControl("Label6");
            Label6.Text = stu.StudentNumber.Trim();
            Label Label5 = (Label)e.Item.FindControl("Label5");
            Label5.Text = stu.StudentName.Trim();

            Exam.Core.Model.StudentExam model = examDal.getStudentScoreByStuidPaperid(int.Parse(_id),paperid);
            if (model.ExamStatus.Trim() == "2")
            {
                Label5.Text = "<a href='examinfo.aspx?stuid=" + model.StudentId.Trim() + "&paperid=" + model.PaperId.Trim() + "&p=" + Server.UrlEncode(model.StudentPaper.Trim()) + "' target=_blank>" + stu.StudentName.Trim() + "</a>";
            }

            Label Label2 = (Label)e.Item.FindControl("Label2");
            Label2.Text = model.TotalScore.Trim();
            Label Label3 = (Label)e.Item.FindControl("Label3");
            Label3.Text = model.TotalScorePer.Trim();
            Label Label4 = (Label)e.Item.FindControl("Label4");
            string status = model.ExamStatus.Trim();
            if (status.Trim() == "0")
            {
                Label4.Text = Resources.labels.Notstarted;
            }
            if (status.Trim() == "1")
            {
                Label4.Text = Resources.labels.BeganExam;
            }
            if (status.Trim() == "2")
            {
                Label4.Text = Resources.labels.submitpaper;
            }
            if (status.Trim() == "3")
            {
                Label4.Text = "培训师未判卷";
            }
            if (status.Trim() == "4")
            {
                Label4.Text = "培训师判卷给分";
            }

            //0未开始 1开始考试 2已经交卷 3培训师未判卷 4培训师判卷给分
            //Literal litTj = (Literal)e.Item.FindControl("litTj");
            //litTj.Text = sb.ToString();
        }

    }

    protected string getDepart(string id)
    {
        string result = "";
        int i = 0;
        foreach (Exam.Core.Model.Department item in Departlist)
        {
            //排除第一个
            //if (item.Text == "所有单位")
            if (i == 0)
            {
                i++;
                continue;
            }

            if (id.Trim() == item.DepartmentID.Trim())
            {
                result = item.DepartmentName.Trim();
                break;
            }
        }
        return result.Trim();
    }

}